package com.gxa.saas.controller;


import com.gxa.saas.pojo.StoreImages;
import com.gxa.saas.pojo.Upload;
import com.gxa.saas.service.StoreImagesService;
import com.gxa.saas.service.UploadManageService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Api("行为上传的相关操作")
@RestController
@RequestMapping("/uploadManage")
@CrossOrigin
public class UploadManageController {

    @Autowired
    UploadManageService uploadManageService;

    @Autowired
    StoreImagesService storeImagesService;


    @ApiOperation(value = "分页查询",produces = "query")
    @PostMapping("/selectByPage")
    public Map selectByPage(Integer page){
        List<Upload> uploads1 = uploadManageService.selectAll();
        List<Upload> uploads = uploadManageService.selectByPage(page, 8);


        jsonResult.setData(uploads);
        jsonResult.setCode("200");
        jsonResult.set("total",uploads1.size());

        return jsonResult.getMap();
    }
    JsonResult jsonResult=new JsonResult();

    @ApiOperation(value = "删除",produces = "query")
    @PostMapping("delete")
    public Map delete(Integer id){

        Upload upload = uploadManageService.selectById(id);
        storeImagesService.delete(upload.getName());
        uploadManageService.delete(id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");

        return jsonResult.getMap();
    }

    @ApiOperation(value = "根据条件查询",produces = "query")
    @PostMapping("/selectByName")
    public Map selectByName(String from,String to,String person,String name){
        Map map=new HashMap();
        map.put("name",name);
        map.put("person",person);
        map.put("from",from);
        map.put("to",to);
        List<Upload> uploads = uploadManageService.selectByName(map);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(uploads);

        return  jsonResult.getMap();
    }

    @ApiOperation(value = "上传")
    @PostMapping("/upload")
    public Map upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        //QQ截图20200724153059.png
        String originalFilename = multipartFile.getOriginalFilename();
        System.out.println("originalFilename:" + originalFilename);
        // 从原始文件名中获取后缀,作为新添加的文件的文件名后缀(匹配)
        String subfix = originalFilename.substring(originalFilename.indexOf(".") + 1);
        System.out.println("subfix:" + subfix);
        //使用UUID作为文件名,保证服务器中存放的文件名唯一
        String targetFile = UUID.randomUUID().toString().replace("-", "") + "." + subfix;
        System.out.println("targetFile:" + targetFile);
        // 通过request获取当前程序的根路径"/"

        String path = request.getSession().getServletContext().getRealPath("/");
        // 通过绝对路径
        //String path =("E:");
        String fileName = path + "/images/uploadImg/" + targetFile;
        System.out.println("fileName:" + fileName);
        // 构建文件对象
        File uploadFile = new File(fileName);
        //核心操作(含有异常操作, 利用异常设定状态码(和前端约定)
        // 此处应当和ExceptionController结合, 当try执行成功则设定状态码0
        // 出现异常则设定其他
        // 再将异常信息结合BusinessException抛出, 交给ExceptionController处 理
        try {
            multipartFile.transferTo(uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(fileName);

        return jsonResult.getMap();
    }

}
